package context.dao;

import context.model.Comment;
import context.model.User;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class CommentRepository {
    private final String SELECT_BY_ID = "SELECT comment_userid,commentcontent,user.username,userinformation.img FROM contest.comment join user on comment_userid=user.id join userinformation on comment_userid=userinformation.userid where dynamicid=?";
    private final String SELECT_BY_USERID = "select * from comment where id=?";
    private final String INSERT_USER = "insert into comment(comment_userid,commentcontent,dynamicid) values(?,?,?)";

    private JdbcTemplate jdbcTemplate;
    private Connection connection;
    @Autowired          //自动装配
    public CommentRepository(JdbcTemplate jdbcTemplate) throws SQLException {
        this.jdbcTemplate = jdbcTemplate;
        connection = jdbcTemplate.getDataSource().getConnection();
    }

    @Autowired
    private UserinformationRepository mUserinformationRepo;

    @Autowired
    private UserRepository mUserRepo;


    private Comment mapRow(ResultSet resultSet) throws SQLException {
        Comment result = new Comment();
        result.setCommentUsername(resultSet.getString("username"));
        result.setCommentContent(resultSet.getString("commentcontent"));
        result.setCommnetUserImg(resultSet.getString("img"));
        return result;
    }


    //增
    public void addComment(final long comment_userid, final String commentcontent, final long dynamicid) {

        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(INSERT_USER);
            ps.setLong(1, comment_userid);
            ps.setString(2, commentcontent);
            ps.setLong(3, dynamicid);
            int i = ps.executeUpdate();
            if (i > 0) {
                System.out.println("插入成功");
            } else {
                System.out.println("插入失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    //删
    private static final String sql = "delete  from comment where commentid = ?";

    public void delete(int commentid) {
        Object[] obj = new Object[]{commentid};
        int i = jdbcTemplate.update(sql, obj);

        if (i > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

    public List<Comment> getDynamicCommnents(long dynamicID) {
        long t1 = System.currentTimeMillis();
        List<Comment> comments = new ArrayList<>();
        try {
            //Connection connection=jdbcTemplate.getDataSource().getConnection();

            PreparedStatement preparedStatement = connection.prepareStatement(SELECT_BY_ID);
            preparedStatement.setLong(1, dynamicID);

            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Comment comment = mapRow(resultSet);
//                comment.setCommnetUserImg(mUserinformationRepo.getUserimg(comment.getCommnent_userid()));
//                comment.setCommentUsername(mUserRepo.getUsername(comment.getCommnent_userid()));
                comments.add(comment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.print("动态id:" + dynamicID);
        System.out.println("耗时" + (System.currentTimeMillis() - t1));
        return comments;
    }
}
